package utils.pool
{
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	import flash.utils.getQualifiedClassName;
	
	import utils.IClear;
	
	public class ObjectPool extends EventDispatcher
	{
		private static var _poolDic				: Dictionary = new Dictionary();

		public function ObjectPool()
		{
		}

		public static function push(target:*):void
		{
			if(target == null) return;
			var className:String = getQualifiedClassName(target);
			if (_poolDic[className] == null)
			{
				_poolDic[className] = [];
			}
			if (target != null)
			{
				if(target is IClear) target.clear();
				_poolDic[className].push(target);
			}
		}
		public static function pop(cls:*):*
		{
			if(cls == null) return null;
			var className:String;
			if(cls is Class) className = getQualifiedClassName(cls);
			else if(cls is String) className = cls;
			else return null;
			var target:Object;
			if (_poolDic[className] != null && _poolDic[className].length > 0)
			{
				target = _poolDic[className].pop();
			}
			else
			{
				target = new cls;
			}
			return target;
		}
		public static function clear(objects:Array):void {
			while(objects.length > 0) {
				push(objects.pop());
			}
		}
	}
}